package com.lyf.spring.demo.controller;

import com.lyf.spring.demo.model.MessageInfo;
import com.lyf.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")//Request包括Post和Get这些
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;
//    private List<MessageInfo> messageInfoList=new ArrayList<>();

    @RequestMapping(value = "/publish",produces = "application/json")
    //如果返回对象上面不用写produces后面
    //RequestBody从请求正文里面拿数据
    public String publish(@RequestBody MessageInfo messageInfo){
        if(!StringUtils.hasLength(messageInfo.getFrom())
        ||!StringUtils.hasLength(messageInfo.getMessage())
        ||!StringUtils.hasLength(messageInfo.getTo())){
            return "{\"ok\":0}";
        }
        //存储留言
        messageService.addMessage(messageInfo);
//        messageInfoList.add(messageInfo);
        return "{\"ok\":1}";
    }
    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.queryAll();
    }
}
